package com.tongjie.hotelbooking.init;

import com.tongjie.hotelbooking.authority.entity.Authority;
import com.tongjie.hotelbooking.authority.repository.AuthorityRepository;
import com.tongjie.hotelbooking.role.entity.Role;
import com.tongjie.hotelbooking.role.repository.RoleRepository;
import com.tongjie.hotelbooking.user.entity.User;
import com.tongjie.hotelbooking.user.enums.UserStatus;
import com.tongjie.hotelbooking.user.enums.UserType;
import com.tongjie.hotelbooking.user.repository.UserRepository;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.Arrays;
import java.util.Date;
import java.util.UUID;

@Service
public class InitService {

    @Resource
    private UserRepository userRepository;

    @Resource
    private RoleRepository roleRepository;

    @Resource
    private AuthorityRepository authorityRepository;

    @Resource
    private PasswordEncoder passwordEncoder;

    @Transactional
    public void initUserRoleAuthority() {
        User user = this.userRepository.getUserByUsername("admin");
        if (user != null) {
            return;
        }

        Authority authorityUser = Authority.builder().authorityName("用户管理").authorityCode("user").description("用户管理").build();
        Authority authorityUserAdd = Authority.builder().authorityName("用户新增").authorityCode("user_add").description("用户新增").build();
        Authority authorityUserDelete = Authority.builder().authorityName("用户删除").authorityCode("user_delete").description("用户删除").build();
        Authority authorityUserUpdate = Authority.builder().authorityName("用户修改").authorityCode("user_update").description("用户修改").build();
        Authority authorityMerchant = Authority.builder().authorityName("顾问管理").authorityCode("merchant").description("顾问管理").build();
        Authority authorityMerchantAdd = Authority.builder().authorityName("顾问新增").authorityCode("merchant_add").description("顾问新增").build();
        Authority authorityMerchantDelete = Authority.builder().authorityName("顾问删除").authorityCode("merchant_delete").description("顾问删除").build();
        Authority authorityMerchantUpdate = Authority.builder().authorityName("顾问修改").authorityCode("merchant_update").description("顾问修改").build();
        Authority authorityIssue = Authority.builder().authorityName("需求管理").authorityCode("issue").description("需求管理").build();
        Authority authorityIssueAdd = Authority.builder().authorityName("需求新增").authorityCode("issue_add").description("需求新增").build();
        Authority authorityIssueDelete = Authority.builder().authorityName("需求删除").authorityCode("issue_delete").description("需求删除").build();
        Authority authorityIssueUpdate = Authority.builder().authorityName("需求更新").authorityCode("issue_update").description("需求更新").build();
        Authority authorityOrder = Authority.builder().authorityName("订单管理").authorityCode("order").description("订单管理").build();
        Authority authorityBill = Authority.builder().authorityName("账单管理").authorityCode("bill").description("账单管理").build();
        Authority authorityProfile = Authority.builder().authorityName("个人设置").authorityCode("profile").description("个人设置").build();
        this.authorityRepository.saveAll(Arrays.asList(
                authorityUser, authorityUserAdd, authorityUserDelete, authorityUserUpdate,
                authorityMerchant, authorityMerchantAdd, authorityMerchantDelete, authorityMerchantUpdate,
                authorityIssue, authorityIssueAdd, authorityIssueDelete, authorityIssueUpdate,
                authorityOrder, authorityBill, authorityProfile)
        );

        Role role = Role.builder()
                .roleName("系统管理员")
                .roleCode("role_admin")
                .description("系统管理员")
                .authorities(Arrays.asList(
                        authorityUser, authorityUserAdd, authorityUserDelete, authorityUserUpdate,
                        authorityMerchant, authorityMerchantAdd, authorityMerchantDelete, authorityMerchantUpdate,
                        authorityIssue, authorityIssueAdd, authorityIssueDelete, authorityIssueUpdate,
                        authorityOrder, authorityBill, authorityProfile
                ))
                .build();
        this.roleRepository.save(role);

        User adminUser = User.builder()
                .username("admin")
                .password(passwordEncoder.encode("password"))
                .userType(UserType.ADMIN)
                .userStatus(UserStatus.ACTIVITED)
                .nickName("admin")
                .mobile("13482432954")
                .uuid(UUID.randomUUID().toString())
                .registerTime(new Date())
                .registerIp("0.0.0.0")
                .lastLoginIp("0.0.0.0")
                .createTime(new Date())
                .lastUpdateTime(new Date())
                .wxOpenId("")
                .roles(Arrays.asList(role)).build();
        this.userRepository.save(adminUser);
    }

}
